<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSV Import and Export Methods : Included Extensions : DataMapper ORM - User Guide</title>

<style type="text/css" media="all">@import url('../../css/userguide.css');</style>
<link rel="shortcut icon" type="image/png" href="../../images/favicon.png" />
<link rel="stylesheet" type="text/css" media="all" href="../../css/userguide.css" />
<link rel="alternate" type="application/rss+xml" title="Datamapper ORM Updates Feed" href="/rss.xml" />

<meta http-equiv="expires" content="-1" />
<meta http-equiv= 'pragma' content="no-cache" />
<meta name="robots" content="all" />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper ORM</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="/">Datamapper ORM Home</a> &nbsp;&#8250;&nbsp;
<a href="../../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
<a href="../extlist.html">Included Extensions</a> &nbsp;&#8250;&nbsp;
CSV Import and Export Methods
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>CSV Import and Export Methods (csv)</h1>
<p class="note">To enable these methods, add <dfn>'csv'</dfn> to DataMapper's config, under <strong>'extensions'</strong>.</p>
<p>Convert arrays of DataMapper models to and from CSV files.  This extension uses the built-in PHP <a href="http://www.php.net/manual/en/function.fgetcsv.php">fgetcsv</a> and <a href="http://www.php.net/manual/en/function.fputcsv.php">fputcsv</a>.  Many issues with CSV formats can be resolved by checking the PHP manual.</p>

<h2>csv_export($filename, <i>$fields</i>, <i>$include_header</i>)</h2>
<ul>
	<li><dfn>$filename</dfn>: The name of the file to save to, or an open handle.  If this is a string, the file will be overwritten.  If it is a handle, it will not be closed.</li>
	<li><dfn>$fields</dfn>: (Optional) If provided, only these fields will be exported.  If empty or not provided, all of the database columns will be exported.</li>
	<li><dfn>$include_header</dfn>: (Optional) If <dfn>TRUE</dfn>, the column names will be output on the first row (recommended).  Defaults to <dfn>TRUE</dfn>.</li>
	<li><b>Returns</b>: <dfn>TRUE</dfn> or <dfn>FALSE</dfn> if an error occurs, or the array of objects is empty.</li>
</ul>
<p>Outputs the <dfn>$fields</dfn> within <var>$object<kbd>-></kbd><i>all</i></var> to a CSV file.  If <var>$object<kbd>-></kbd><i>all</i></var> is an empty array, nothing is written and <dfn>FALSE</dfn> is returned.</p>

<h3>Usage</h3>
<pre>
<var>$u </var><kbd>= new </kbd><var>User</var><kbd>();
</kbd><samp>// load all users
</samp><var>$u</var><kbd>-&gt;</kbd><var><u>get</u></var><kbd>();
</kbd><samp>// Output $u-&gt;all to /tmp/output.csv, using all database fields.
</samp><var>$u</var><kbd>-&gt;</kbd><var><s>csv_export</s></var><kbd>(</kbd><dfn>'/tmp/output.csv'</dfn><kbd>);</kbd>
</pre>

<h2>csv_import($filename, <i>$fields</i>, <i>$header_row</i>, <i>$callback</i>)</h2>
<ul>
	<li><dfn>$filename</dfn>: The name of the file to save to, or an open handle.  If this is a string, the file will be overwritten.  If it is a handle, it will not be closed.</li>
	<li><dfn>$fields</dfn>: (Optional) If provided, only these fields will be imported.  If empty or not provided, all of the database columns will be imported.</li>
	<li><dfn>$header_row</dfn>: (Optional) If <dfn>TRUE</dfn>, the first row is assumed to be a header containing the field names.  Defaults to <dfn>TRUE</dfn>.</li>
	<li><dfn>$callback</dfn>: (Optional) A <a href="http://us.php.net/manual/en/language.pseudo-types.php#language.types.callback">callback method</a> for each row.  The provided method will be called with the new object as the argument.  The method should return <dfn>FALSE</dfn> if the item could not be imported, the string <dfn>'stop'</dfn> to stop importing, or <dfn>TRUE</dfn> on success.</li>
	<li><b>Returns</b>: If a callback method was specified, the number of successfully processed objects.  Otherwise, an array containing the (unsaved) imported DataMapper objects.</li>
</ul>
<p>Imports a CSV file into DataMapper objects.</p>
<p class="important"><strong><em>Important:</em></strong> If the CSV file has an <b>id</b> column, you either need to exclude it from the list of fields, or use the <var><u>save_as_new</u></var> method.</p>

<h3>Usage</h3>
<pre>
<kbd>function </kbd><var>import_csv</var><kbd>()
{
    </kbd><samp>// Save the CSV file, place the filename into $csvfile
    </samp><var>$n </var><kbd>= new </kbd><var>Note</var><kbd>();
    </kbd><samp>// After each row is parsed, $this-&gt;_save_note will be called with the new Note.
    </samp><var>$saved_notes </var><kbd>= </kbd><var>$n</var><kbd>-&gt;</kbd><var><s>csv_import</s></var><kbd>(</kbd><var>$csvfile</var><kbd>, array(</kbd><dfn>'message'</dfn><kbd>), </kbd><var><dfn>TRUE</dfn></var><kbd>, array(</kbd><var><b>$this</b></var><kbd>, </kbd><dfn>'_save_note'</dfn><kbd>));
    echo </kbd><dfn>"$saved_notes Notes were imported."</dfn><kbd>;
}
function </kbd><var>_save_note</var><kbd>(</kbd><var>$note</var><kbd>)
{
    if( ! </kbd><var>$note</var><kbd>-&gt;</kbd><var><u>save</u></var><kbd>())
    {
        echo(</kbd><var>$note</var><kbd>-&gt;</kbd><var><i>error</i></var><kbd>-&gt;</kbd><var>string</var><kbd>);
        return </kbd><var><dfn>FALSE</dfn></var><kbd>;
    }
    return </kbd><var><dfn>TRUE</dfn></var><kbd>;
}</kbd>
</pre>

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<span id="footer_previous">Previous Topic:&nbsp;&nbsp;<a href=""></a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;</span>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../../index.html">User Guide Home</a>
<span id="footer_next">&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href=""></a></span>
</p>
<div id="copyrights">
<p><a href="/">Datamapper ORM</a> &nbsp;&middot;&nbsp; Copyright &copy; 2010-2011 &nbsp;&middot;&nbsp; Harro "WanWizard" Verton</p>
<p><a href="../license.html">Other License Information</a></p>
</div>
</div>

<script type="text/javascript" src="../../js/mootools.js"></script>
<script type="text/javascript" src="../../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../../',
			pagespath: '../',
			last: 'extlist'
		});

	});
//-->
</script>
</body>
</html>
